//
//  TabbarControllerViewController.swift
//  WeiBo_NB
//
//  Created by apple on 15/3/5.
//  Copyright (c) 2015年 Ai. All rights reserved.
//

import UIKit

class TabbarControllerViewController: UITabBarController {

    @IBOutlet weak var AITabBar: TabBar!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 依次添加四个控制器
        addchildControllers()
        // 解决循环引用
        weak var weakself = self
        // 定义回调
        AITabBar.composeBtnClicked = {
            let composeSB = UIStoryboard(name: "Compose", bundle: nil)
            let composeVC = composeSB.instantiateInitialViewController() as! UIViewController

            weakself!.presentViewController(composeVC, animated: true, completion: nil)
        }
    }
    
    deinit{
        println("gone----over")
    }
    
    // 依次添加四个控制器
    func addchildControllers(){
        addNewViewController("Home", title: "首页", imageName: "tabbar_home", highlightImage: "tabbar_home_highlighted")
        addNewViewController("Message", title: "消息", imageName: "tabbar_message_center", highlightImage: "tabbar_message_center_highlighted")
        addNewViewController("Discover", title: "发现", imageName: "tabbar_discover", highlightImage: "tabbar_discover_highlighted")
        addNewViewController("Profile", title: "我", imageName: "tabbar_profile", highlightImage: "tabbar_profile_highlighted")
        
    }
    
    // 往tabbar里面添加不同控制器的方法
    func addNewViewController(name:String,title:String,imageName:String,highlightImage:String){
        
        let sb = UIStoryboard(name: name, bundle: nil)
        let vc = sb.instantiateInitialViewController() as!UINavigationController
//        vc.tabBarItem.title = title
        vc.title = title
        vc.tabBarItem.image =  UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: highlightImage)
        
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName :UIColor.orangeColor()], forState: UIControlState.Selected)
        self.addChildViewController(vc)
        
    }

}
